<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import {errorModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		data() {
			return buildSubFormData({
				userCacheKey:"hr-zz-tpromoteEvaluateMulti-title_salary-user-cache",
				existId:'',
				isTrue:false,
				pemId:''
			})
		},
		methods:{
			onSelectPromotion(value){
				let _that = this
				let date  = moment(value).format('YYYY-MM-DD')
				if(_that.$refs.autoform.formdata.probationEndDate){
					if(moment(_that.$refs.autoform.formdata.probationEndDate).isBefore(date)){
						let title = _that.$refs.autoform.getEditorTitle("title_salary","probationEndDate")
						_that.om.toast("请选择"+title+"之前的日期")
						_that.$refs.autoform.clear("title_salary","promotionDate")
					}
				}
				if(_that.$refs.autoform.formdata.workDate){
					if(moment(_that.$refs.autoform.formdata.workDate).isAfter(date)){
						let title = _that.$refs.autoform.getEditorTitle("title_salary","workDate")
						_that.om.toast("请选择"+title+"之后的日期")
						_that.$refs.autoform.clear("title_salary","promotionDate")
					}
				}
			},
			onInputSalary2(){
				let _that = this
				let salaryList = []
				let num = 0
				for(let index in _that.$refs.autoform.formdata){
					if(index.indexOf('salary') != -1){
						if(_that.containsNumber(index.substr(6)) ){
							salaryList.push(index)
						}
					}
				}
				for(let sIndex in salaryList){
					let n = Number(salaryList[sIndex].substr(6))
					if(n > 9 && n <= 27){
						num += Number(_that.$refs.autoform.formdata[salaryList[sIndex]])
					}
				}
				_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'monthSalaryStandard', Number(_that.$refs.autoform.formdata.salary1) + num)
			},
			containsNumber(str) {
				var reg=/\d/;
				return reg.test(str);
			},
			onInputSalary(){
				let _that = this
				let salaryList = []
				let num = 0
				for(let index in _that.$refs.autoform.formdata){
					if(index.indexOf('salary') != -1){
						if(_that.containsNumber(index.substr(6)) ){
							salaryList.push(index)
						}
					}
				}
				for(let sIndex in salaryList){
					let n = Number(salaryList[sIndex].substr(6))
					if( ((n > 1) && (n < 9)) || ((n > 27) && (n < 40)) ){
						num += Number(_that.$refs.autoform.formdata[salaryList[sIndex]])
					}
				}
				_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'salary9', Number(_that.$refs.autoform.formdata.salary1) - num)
			},
			getUserInfo(id){
				let _that = this
				_that.om.request(
					"tpromoteEvaluateMulti/getUserInfo",
					{
						userIdStr: id
					}
				).then(res=>{
					if(res.value.length){
						Object.assign(_that.$refs.autoform.formdata,res.value[0])
						if(_that.$refs.autoform.formdata.probationEndDate){
							if(!_that.$refs.autoform.formdata.promotionDate){
								_that.$refs.autoform.changeEditor("title_salary","promotionDate","max",_that.$refs.autoform.formdata.probationEndDate)
								_that.$refs.autoform.formdata.promotionDate = _that.$refs.autoform.formdata.probationEndDate
							}
							if(_that.isTrue){
								let startDate = moment().startOf('month').format('YYYY-MM-DD');
								let nextTime = moment().add(1,'month').format('YYYY-MM-DD')
								let endDate = moment(nextTime).endOf('month').format('YYYY-MM-DD')
								if(!moment(_that.$refs.autoform.formdata.probationEndDate).isBetween(startDate,endDate)){
									let title  = _that.$refs.autoform.getEditorTitle("title_salary","salaryEffectDate")
									errorModal(_that,"提示信息","管控参数约束了"+title+"只能选择当月或下月的日期，请重新选择",()=>{
										_that.$refs.autoform.clear("title_salary","salaryEffectDate")
									})
								} 
							}
						}
					}
				})
			},
			verifyUser(id){
				let _that = this
				_that.om.request(
					"tpromoteEvaluate/verifyUser",
					{
						'userId':id,
						'pemId':_that.pemId
					}
				).then(res=>{
					if(res.value){
						errorModal(_that,'提示信息',res.value,()=>{})
					}else{
						_that.getUserInfo(id)
					}
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.verifyUser(d.id)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.existId = _that.datas.filter((e,i)=>{
					return (i != _that.index)
				}).map(e=>e.userId).join(",")
				_that.pemId = e.editId
				if(e.isTrue){
					_that.isTrue = true
				}
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
								changeEditor("title_salary","userId",{
									link:"../../public/search/search",
									"linkParam":{
										other:JSON.stringify({
											//zposId: e.zposId,
											noUserIdStr:_that.existId
										}),
										method:'post',
										url:'tpromoteEvaluateMulti/getUserList',
										namefield:'username',
										cachename:_that.userCacheKey,
										initload:true
									}
								},null,_that.autoformdata)
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
